package hu.genius.view;

import hu.genius.CoreApplication;
import hu.genius.R;
import hu.genius.R.id;
import hu.genius.R.layout;
import hu.genius.model.BasketManager;
import hu.genius.model.entity.Order;
import hu.genius.model.entity.Product;
import hu.genius.view.adapters.BasketItemAdapter;
import hu.genius.view.utils.ViewUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;

import android.app.ListActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.TextView;

public class BasketActivity extends ListActivity implements OnClickListener {

	private BasketManager basketManager;
	private TextView totalAmmount;
	private int totalPrice = 0;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.basket);

		basketManager = ((CoreApplication) this.getApplication())
				.getBasketManager();
		Log.i("BasketActivity", basketManager.toString());

		totalAmmount = (TextView) findViewById(R.id.totalAmmountTextView);

		Button backButton = (Button) findViewById(R.id.basketBackButton);
		backButton.setOnClickListener(this);

		Button refreshButton = (Button) findViewById(R.id.basketRefreshButton);
		refreshButton.setOnClickListener(this);

		Button clearAllButton = (Button) findViewById(R.id.clearAllButton);
		clearAllButton.setOnClickListener(this);

		updateUI();
	}

	public void back(View view) {
		this.finish();
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.basketBackButton: {
			Log.d("BasketActivity", "click basketBack button.");
			this.finish();
			break;
		}
		case R.id.clearAllButton: {
			Log.d("BasketActivity", "click clearAll button.");
			if (basketManager != null) {
				basketManager.resetOrderMap();
				updateUI();
			}
			break;
		}
		case R.id.basketRefreshButton: {
			Log.d("BasketActivity", "click refresh button.");
			if (basketManager != null) {
				updateUI();
			}
			break;
		}
		}
	}

	private void updateUI() {
		Log.d("BasketActivity", "updateUI - " + basketManager.toString());

		ListAdapter ordersListAdaptor = new BasketItemAdapter(this,
				R.layout.listview_basket_item_layout, generateOrdersList());
		totalAmmount.setText(totalPrice + " Ft");
		this.setListAdapter(ordersListAdaptor);
	}

	private List<Order> generateOrdersList() {
		totalPrice = 0;
		List<Order> orders = new ArrayList<Order>(basketManager.getOrderMap()
				.size());
		for (Entry<Product, Integer> entry : basketManager.getOrderMap()
				.entrySet()) {
			Order order = new Order();
			order.setProduct(entry.getKey());
			order.setQuantity(entry.getValue());
			totalPrice += (order.getProduct().getPrice() * order.getQuantity());
			orders.add(order);
		}
		return orders;
	}

	@Deprecated
	private View buildBasketContentList() {
		Log.i("BasketActivity", "buildBasketContentList - " + basketManager.toString());

		LinearLayout actionLayout = new LinearLayout(this);
		actionLayout.setOrientation(LinearLayout.VERTICAL);
		actionLayout.setBackgroundColor(Color.WHITE);

		for (Entry<Product, Integer> entry : basketManager.getOrderMap()
				.entrySet()) {
			Product product = entry.getKey();

			LinearLayout rowLayout = new LinearLayout(this);
			rowLayout.setOrientation(LinearLayout.HORIZONTAL);

			ImageView productView = new ImageView(this);
			productView.setImageBitmap(ViewUtils.loadImage(this,
					product.getId() + "s110c" + product.getPrice() + ".00i"));
			productView.setLayoutParams(new LinearLayout.LayoutParams(ViewUtils
					.getDpSize(this, 110), ViewUtils.getDpSize(this, 110)));
			rowLayout.addView(productView);

			LinearLayout infoLayout = new LinearLayout(this);
			infoLayout.setOrientation(LinearLayout.HORIZONTAL);

			LinearLayout textLayout = new LinearLayout(this);
			textLayout.setOrientation(LinearLayout.VERTICAL);

			TextView nameView = new TextView(this);
			nameView.setText(product.getName());
			nameView.setTextColor(Color.BLACK);
			textLayout.addView(nameView);

			TextView priceView = new TextView(this);
			priceView.setText(product.getPrice() + "");
			priceView.setTextColor(Color.BLACK);
			textLayout.addView(priceView);

			infoLayout.addView(textLayout);

			rowLayout.addView(infoLayout);
			actionLayout.addView(rowLayout);
		}
		return actionLayout;
	}
}